package cn.jtfadmin.base.lang.common.utils;


import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;


import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.util.*;

/**
 * pdf Utils
 * @author jtf
 * @since 0.0.1
 */
public class PDFUtils {


    /**
     * 获取PDF 文本
     * @param path path
     * @return pdf 文本
     */
    public static String getPDFText(Path path){
        Objects.requireNonNull(path, "获取pdf文本内容，文件路径不能为空");
        return getPDFText(path.toFile());
    }

    /**
     * 获取PDF 文本
     * @param file file
     * @return pdf 文本
     */
    public static String getPDFText(File file){
        Objects.requireNonNull(file, "获取pdf文本内容，文件不能为空");
        try {
            return getPDFText(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException("获取pdf文本内容，文件不存在");
        }
    }

    /**
     * 获取PDF 文本
     * @param inputStream inputStream
     * @return pdf 文本
     */
    public static String getPDFText(InputStream inputStream){
        Objects.requireNonNull(inputStream, "获取pdf文本内容，输入流不能为空");
        PDDocument pdDocument = null;
        try {
            pdDocument = PDDocument.load(inputStream);
            PDFTextStripper pdfStripper = new PDFTextStripper();
            String text = pdfStripper.getText(pdDocument);
            return text;
        }catch (Exception e){
            throw new IllegalArgumentException("获取pdf文本内容",e);
        }finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(pdDocument);
        }

    }


    /**
     * 获取PDF 图片
     * @param path path
     * @return pdf 文本
     */
    public static List<BufferedImage> getPDFImg(Path path){
        Objects.requireNonNull(path, "获取pdf图片内容，文件路径不能为空");
        return getPDFImg(path.toFile());
    }

    /**
     * 获取PDF 图片
     * @param file file
     * @return pdf 文本
     */
    public static List<BufferedImage> getPDFImg(File file){
        Objects.requireNonNull(file, "获取pdf图片内容，文件不能为空");
        try {
            return getPDFImg(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException("获取pdf图片内容，文件不存在");
        }
    }
    /**
     * 获取PDF 图片
     * @param inputStream inputStream
     * @return pdf 文本
     */
    public static List<BufferedImage> getPDFImg(InputStream inputStream){
        Objects.requireNonNull(inputStream, "获取pdf图片内容，输入流不能为空");
        List<BufferedImage> result = new ArrayList<>();
        PDDocument pdDocument = null;
        try {
            pdDocument = PDDocument.load(inputStream);
            int numberOfPages = pdDocument.getNumberOfPages();
            for (int i = 0; i < numberOfPages; i++) {
                PDPage page = pdDocument.getPage(i);
                PDResources resources = page.getResources();
                Iterable<COSName> xObjectNames = resources.getXObjectNames();
                if(xObjectNames!=null){
                    for (COSName xObjectName : xObjectNames) {
                        if(resources.isImageXObject(xObjectName)){
                            PDImageXObject xObject = (PDImageXObject) resources.getXObject(xObjectName);
                            result.add(xObject.getImage());
                        }
                    }
                }
            }

            return result;
        }catch (Exception e){
            throw new IllegalArgumentException("获取pdf图片内容",e);
        }finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(pdDocument);
        }

    }

}
